#!/usr/bin/env python
# coding: utf-8

# In[ ]:


from PIL import Image, ImageTk
import tkinter as tk
from tkinter import filedialog
import cnntest

# import os
# os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
file_path=""
file_path2=""
result=""
result1=""
result_label=None
current_image_label1 = None  # 全局变量，存储当前显示的图片标签
current_image_label2 = None  # 全局变量，存储当前显示的图片标签
def select_image():
    global file_path
    global file_path2
    file_path1 = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg *.png")])  # 弹出选择图片文件对话框
    file_path2=file_path1
    if file_path1:
        show_image1(window,file_path1)
        file_path=file_path1
def show_image1(window,file_path2):
    global current_image_label1  # 声明要使用的全局变量
    image = Image.open(file_path2)
    image=image.resize((400,200))
    photo = ImageTk.PhotoImage(image)
    if current_image_label1 is not None:
        # 如果当前图片标签存在，则替换图片
        current_image_label1.config(image=photo)
        current_image_label1.image = photo
        current_image_label1.place(x=20, y=100)
    else:
        # 如果当前图片标签不存在，则创建新的图片标签并显示图片
        current_image_label1 = tk.Label(window, image=photo)
        current_image_label1.image = photo
        current_image_label1.place(x=20, y=100)
def show_image2():
    global current_image_label2  # 声明要使用的全局变量
    global file_path2
    global result1
    global result2
    result1=cnntest.predect_result(file_path2)
    image = Image.open('./fenge/fenge.jpg')
    image=image.resize((250,130))
    photo = ImageTk.PhotoImage(image)
    if current_image_label2 is not None:
         #如果当前图片标签存在，则替换图片
        current_image_label2.config(image=photo)
        current_image_label2.image = photo
        current_image_label2.place(x=620, y=100)
    else:
        # 如果当前图片标签不存在，则创建新的图片标签并显示图片
        current_image_label2 = tk.Label(window, image=photo)
        current_image_label2.image = photo
        current_image_label2.pack()
        current_image_label2.place(x=620, y=100)
def result_print():
    global result_label
    result_label= tk.Label(window, text=result1+'='+str(eval(result1))+"     ",font=("Microsoft YaHei UI",12))
    result_label.place(x=620, y=300)
if __name__ == '__main__':
    # 创建主窗口
    window = tk.Tk()
    window.title("手写公式识别计算系统")
    window.geometry("1000x600")  # 设置窗口大小和位置，格式为"宽x高+左边距+上边距"
    # 创建选择图片按钮
    select_button = tk.Button(window, text="选择图片", command=select_image)
    select_button.place(x=20, y=20)  # 使用place()方法布局按钮，并设置坐标位置
    # 创建选择图片按钮
    select_button1 = tk.Button(window, text="字符分割", command=show_image2)
    select_button1.place(x=120, y=20)  # 使用place()方法布局按钮，并设置坐标位置
    # 创建选择图片按钮
    select_button2 = tk.Button(window, text="识别计算", command=result_print)
    select_button2.place(x=220, y=20)  # 使用place()方法布局按钮，并设置坐标位置
    label1 = tk.Label(window, text="选择的公式图像：",font=("Microsoft YaHei UI",12))
    label1.place(x=20,y=60)
    label2 = tk.Label(window, text="字符分割后公式图像：",font=("Microsoft YaHei UI",12))
    label2.place(x=620, y=60)
    label3 = tk.Label(window, text="识别公式信息：",font=("Microsoft YaHei UI",12))
    label3.place(x=620, y=260)
    # 运行主循环
    window.mainloop()


# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:




